package org.weso.acota.dao.ontology;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.weso.acota.exceptions.AcotaModelException;
import org.weso.acota.loader.AcotaModelWrapper;
import org.weso.acota.loader.JenaOWLModelWrapper;
import org.weso.acota.loader.resources.FilesResourceLoader;
import org.weso.acota.loader.resources.ResourceLoader;

import com.hp.hpl.jena.ontology.OntModel;

public class FileOntologyDAOImpl implements OntologyDAO {
	
	protected static Logger logger = Logger.getLogger(FileOntologyDAOImpl.class);
	private AcotaModelWrapper ontModel;
	
	public AcotaModelWrapper load(ResourceLoader loader) {
		JenaOWLModelWrapper wrapper = new JenaOWLModelWrapper(loader,"N3"); //FIXME
		this.ontModel = wrapper;
		return wrapper;
	}

	public boolean save(){	
		try {
			String fileName = "ontology.tmp";
			FileOutputStream fos = new FileOutputStream(
					new File(fileName));
			((OntModel)getModel().getModel()).write(fos);
			fos.close();
		} catch (FileNotFoundException e) {
			logger.error("Saving ontology: creating file", e);
			throw new AcotaModelException(e);
		} //FIMXE
		catch (IOException e) {
			logger.error("Saving ontology: writing", e);
			throw new AcotaModelException(e);
		}
		return Boolean.TRUE;
	}

	public AcotaModelWrapper getModel() {
		if(this.ontModel == null){
			ResourceLoader resourceLoader = 
				new FilesResourceLoader(new String[]{});			
			this.load(resourceLoader);
		}
		return this.ontModel;
	}
	

}
